<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <h2>将URL和Token填写到公众平台开发者中心，即可升级成为开发者账号</h2>
    <img v-bind:src="imgUrl"/>
    <el-form-item label="账号名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="账号名称"></el-input>
    </el-form-item>
    <el-form-item label="原始id" prop="account">
      <el-input v-model="dataForm.account" placeholder="原始id"></el-input>
    </el-form-item>
    <el-form-item label="APPID" prop="appid">
      <el-input v-model="dataForm.appid" placeholder="APPID"></el-input>
    </el-form-item>
    <el-form-item label="Secret" prop="appsecret">
      <el-input v-model="dataForm.appsecret" placeholder="Secret"></el-input>
    </el-form-item>
    <el-form-item label="URL" prop="url">
      <el-input v-model="dataForm.url" :disabled="true" placeholder="URL"></el-input>
    </el-form-item>
    <el-form-item label="Token" prop="token">
      <el-input v-model="dataForm.token" :disabled="true" placeholder="Token"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        imgUrl: '',
        dataForm: {
          id: 0,
          name: '',
          account: '',
          appid: '',
          appsecret: '',
          url: '',
          token: ''
        },
        dataRule: {
          name: [
            { required: true, message: '账号名称不能为空', trigger: 'blur' }
          ],
          account: [
            { required: true, message: '原始id不能为空', trigger: 'blur' }
          ],
          appid: [
            { required: true, message: 'APPID不能为空', trigger: 'blur' }
          ],
          appsecret: [
            { required: true, message: 'Secret不能为空', trigger: 'blur' }
          ],
          url: [
            { required: true, message: 'URL不能为空', trigger: 'blur' }
          ],
          token: [
            { required: true, message: 'Token不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/cms/account/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.data.name
                this.dataForm.account = data.data.account
                this.dataForm.appid = data.data.appid
                this.dataForm.appsecret = data.data.appsecret
                this.dataForm.url = data.data.url
                this.dataForm.token = data.data.token
                this.imgUrl = `http://open.weixin.qq.com/qr/code?username=${data.data.account}`
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cms/account/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'account': this.dataForm.account,
                'appid': this.dataForm.appid,
                'appsecret': this.dataForm.appsecret,
                'url': this.dataForm.url,
                'token': this.dataForm.token
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
